/**
 * Author: luoqi
 * Created Date: 2025-11-13 01:07:53
 * Last Modified: 2025-11-15 02:44:18
 * Modified By: luoqi at <**@****>
 * Copyright (c) 2025 <*****>
 * Description:
 */

#include <bsp.h>
#include <cli.h>
#include <stdlib.h>
#include <tim_port.h>
#include <tim.h>
#include "dbg.h"

static int args_pwm(int argc, char **argv)
{
    if(argc == 3) {
        int ch = atoi(argv[1]);
        if(ch >= PWM_CH1 && ch <= PWM_CH4) {
            int duty = strtol(argv[2], NULL, 10);
            tim_pwm_set(TIM_PORT_PWM1, ch, duty);
        }
    } else {
        return -1;
    }
    return 0;
}

static int args_start(int argc, char **argv)
{
    if(argc == 2) {
        if(ISARG(argv[1], "pwm1")) {
            tim_start(TIM_PORT_PWM1);
        } else if(ISARG(argv[1], "enc2")) {
            tim_start(TIM_PORT_ENC2);
        } else if(ISARG(argv[1], "enc3")) {
            tim_start(TIM_PORT_ENC3);
        } else if(ISARG(argv[1], "tim4")) {
            tim_start(TIM_PORT_TIM4);
        }
    } else {
        return -1;
    }
    return 0;
}

static int args_stop(int argc, char **argv)
{
    if(argc == 2) {
        if(ISARG(argv[1], "pwm1")) {
            tim_stop(TIM_PORT_PWM1);
        } else if(ISARG(argv[1], "enc2")) {
            tim_stop(TIM_PORT_ENC2);
        } else if(ISARG(argv[1], "enc3")) {
            tim_stop(TIM_PORT_ENC3);
        } else if(ISARG(argv[1], "tim4")) {
            tim_stop(TIM_PORT_TIM4);
        }
    } else {
        return -1;
    }
    return 0;
}

static int args_get(int argc, char **argv)
{
    if(argc == 2) {
        if(ISARG(argv[1], "enc2")) {
            TimVal val;
            tim_get(TIM_PORT_ENC2, &val);
            QSH(" enc2 count: %d, overflow: %d\r\n", val.count, val.overflow);
        } else if(ISARG(argv[1], "enc3")) {
            TimVal val;
            tim_get(TIM_PORT_ENC3, &val);
            QSH(" enc3 count: %d, overflow: %d\r\n", val.count, val.overflow);
        } else if(ISARG(argv[1], "tim4")) {
            TimVal val;
            tim_get(TIM_PORT_TIM4, &val);
            QSH(" tim4 count: %d, overflow: %d\r\n", val.count, val.overflow);
        } else {
            return -1;
        }
    } else {
        return -1;
    }
    return 0;
}

static int args_period(int argc, char **argv)
{
    if(argc == 2) {
        if(ISARG(argv[1], "tim4")) {
            QSH("tim4 period: %d\r\n", __HAL_TIM_GET_AUTORELOAD(&htim4) + 1);
        } else {
            return -1;
        }
    }
    if(argc == 3) {
        if(ISARG(argv[1], "tim4")) {
            int period = strtol(argv[2], NULL, 10);
            tim4_set_it_period(period);
        } else {
            return -1;
        }
    } else {
        return -1;
    }
    return 0;
}

static QCliArgsTable table[] = {
    {"pwm1", args_pwm, "[<ch> <duty>]"},
    {"start", args_start, "[pwm1/enc2/enc3/tim4]"},
    {"stop", args_stop, "[pwm1/enc2/enc3/tim4]"},
    {"get", args_get, "[enc2/enc3/tim4]"},
    {"period", args_period, "[<tim4> <ms>]"},
};

static int cmd_tim_hdl(int argc, char **argv)
{
    if(argc == 1) {
        cli_help(table, sizeof(table));
        return 0;
    }
    CLI_ARGS_EXEC(argc, argv, table);
}

int cmd_tim_init(void)
{
    cli_add("tim", cmd_tim_hdl, "TIM command");
    return 0;
}

